package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.*;
import com.zb.mapper.ZjNetworkTeamAuthenFieldValMapper;
import com.zb.service.ZjNetworkTeamAuthenFieldService;
import com.zb.service.ZjNetworkTeamAuthenFieldValOptionService;
import com.zb.service.ZjNetworkTeamAuthenFieldValService;
import com.zb.vo.NetworkFieldValVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 网点团队认证字段值表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-02-01
 */
@Service
public class ZjNetworkTeamAuthenFieldValServiceImpl extends ServiceImpl<ZjNetworkTeamAuthenFieldValMapper, ZjNetworkTeamAuthenFieldVal> implements ZjNetworkTeamAuthenFieldValService {
    @Resource
    private ZjNetworkTeamAuthenFieldService networkTeamAuthenFieldService;
    @Resource
    private ZjNetworkTeamAuthenFieldValOptionService networkTeamAuthenFieldValOptionService;

    // 保存服务商资质认证信息
    @Override
    public void save(Long custAccId, Long networkId, List<NetworkFieldValVo> fieldVals) {
        if (CollectionUtil.isEmpty(fieldVals)) {
            return;
        }
        List<ZjNetworkTeamAuthenFieldVal> adds = new ArrayList<>();
        List<ZjNetworkTeamAuthenFieldVal> updates = new ArrayList<>();
        ZjNetworkTeamAuthenFieldVal val = null;
        List<ZjNetworkTeamAuthenFieldValOption> optionList = new ArrayList<>();
        ZjNetworkTeamAuthenFieldValOption opt = null;
        for (NetworkFieldValVo vo : fieldVals) {
            val = new ZjNetworkTeamAuthenFieldVal();
            BeanUtil.copyProperties(vo, val);
            if (null != val.getId()) {
                updates.add(val);
            } else {
                val.setId(IdWorker.getId());
                val.setCustAccId(custAccId);
                val.setNetworkId(networkId);
                val.setNetworkTeamAuthenFieldId(vo.getFieldId());
                adds.add(val);
            }
            for (FieldValOptionVo optVo : vo.getOptVos()) {
                opt = new ZjNetworkTeamAuthenFieldValOption();
                opt.setId(IdWorker.getId());
                opt.setCustAccId(custAccId);
                opt.setNetworkId(networkId);
                opt.setNetworkTeamAuthenFieldValId(val.getId());
                opt.setOptValue(optVo.getOptValue());
                optionList.add(opt);
            }
        }
        if (CollectionUtil.isNotEmpty(adds)) this.saveBatch(adds);
        if (CollectionUtil.isNotEmpty(updates)) this.updateBatchById(updates);
        networkTeamAuthenFieldValOptionService.deleteByNetworkId(networkId);
        if (CollectionUtil.isNotEmpty(optionList)) networkTeamAuthenFieldValOptionService.saveBatch(optionList);

    }

    // 获取服务商详情之资质认证信息
    @Override
    public List<FieldValVo> getByNetworkId(Long networkId) {
        QueryWrapper<ZjNetworkTeamAuthenFieldVal> wrapper = new QueryWrapper<>();
        wrapper.select("id", "network_team_authen_field_id", "type")
                .in("network_id", networkId);
        List<ZjNetworkTeamAuthenFieldVal> fieldValList = this.list(wrapper);
        if (CollectionUtil.isEmpty(fieldValList)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjNetworkTeamAuthenField> teamAuthenFieldQueryWrapper = new QueryWrapper<>();
        teamAuthenFieldQueryWrapper.select("id", "name");
        List<ZjNetworkTeamAuthenField> authenFieldList = networkTeamAuthenFieldService.list(teamAuthenFieldQueryWrapper);
        List<ZjNetworkTeamAuthenFieldValOption> optionList = networkTeamAuthenFieldValOptionService.getByNetworkId(networkId);
        List<FieldValVo> voList = fieldValList.stream().map(val -> {
            FieldValVo vo = new FieldValVo();
            vo.setId(val.getId());
            vo.setType(val.getType());
            authenFieldList.stream().filter(f -> Objects.equals(f.getId(), val.getNetworkTeamAuthenFieldId())).findFirst()
                    .map(ZjNetworkTeamAuthenField::getName).ifPresent(vo::setName);
            vo.setOptVos(optionList.stream().filter(f -> Objects.equals(f.getNetworkTeamAuthenFieldValId(), val.getId())).map(opt -> {
                return new FieldValOptionVo(opt.getId(), opt.getOptValue());
            }).toList());
            return vo;
        }).toList();
        return voList;
    }


}
